/*
 * Hibiscus is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 3 of the License,
 * or (at your option) any later version.
 *
 * Hibiscus is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Hibiscus; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 *
 * Copyright (C) 2003 Tetete - http://tetete.hu.
 */
package hu.tetete.hibiscus.expression;

import java.io.Serializable;
import java.util.Map;

import hu.tetete.hibiscus.propertyvalue.PropertyValue;

/**
 * A logical <tt>And</tt> operation on the results of two or more expressions.
 */
public class AndExpression implements Expression {

    /** @see Serializable */
    private static final long serialVersionUID = 1L;

    private final Expression[] expressions;

    /**
     * Creates an andExpression.
     * @param firstExpression The first expression (NonNull)
     * @param secondExpression The second expression (NonNull)
     * @param otherExpressions The other expressions (Nullable)
     */
    public AndExpression(final Expression firstExpression, final Expression secondExpression, final Expression... otherExpressions) {
        super();

        if (otherExpressions != null && otherExpressions.length != 0) {
            this.expressions = new Expression[otherExpressions.length + 2];
        } else {
            this.expressions = new Expression[2];
        }

        this.expressions[0] = firstExpression;
        this.expressions[1] = secondExpression;

        if (otherExpressions != null) {
            for (int i = 0; i < otherExpressions.length; i++) {
                this.expressions[i + 2] = otherExpressions[i];
            }
        }
    }

    /**
     * @see Expression#isVisible(java.util.Map)
     */
    @Override
    public boolean isVisible(final Map<Long, PropertyValue> values) {
        for (final Expression expression : this.expressions) {
            if (!expression.isVisible(values)) {
                return false;
            }
        }
        return true;
    }

}
